package com.byxy.ssm.demo.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.byxy.ssm.demo.entity.Forum;
import com.byxy.ssm.demo.entity.Reply;
import com.byxy.ssm.demo.entity.Tab;
import com.byxy.ssm.demo.entity.Tip;
import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.ForumService;
import com.byxy.ssm.demo.service.ReplyService;
import com.byxy.ssm.demo.service.TipService;
import com.byxy.ssm.demo.service.TobService;
import com.byxy.ssm.demo.vo.TipVO;

import lombok.extern.slf4j.Slf4j;
@Controller
@Slf4j//新的输出控制台方法
public class TipController {
	@Autowired
	private TobService tobService;
	@Autowired
	private TipService tipService;
	@Autowired
	private ForumService forumService;
	@Autowired
	private ReplyService replyService;
	@GetMapping("toPublishTipPage.do")//发布新帖
	public String fatie(Model model){
		List<Forum>forums =forumService.findAll();
		List<Tab>Tab =tobService.findAll();
		model.addAttribute("tabs",Tab);
		model.addAttribute("forums",forums);
		return"publishTip";
	}
	
	@PostMapping("publishNewTip.do")
	public String doAdd(Tip tip,String selectedForumId,HttpSession session){
		log.info("tipTitle="+tip.getTipTitle());
		log.info("tipContent="+tip.getTipContent());
		log.info("tabId="+tip.getTabId());
		log.info("selectedForumId="+selectedForumId);
		User user =(User)session.getAttribute("USER");
		tip.setUserId(user.getUserId());
		tip.setTipClick(0);
		tip.setTipIsDeleted(0);
		tip.setTipIsKnot(0);
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tip.setTipReplies(0);
		tipService.add(tip);
		return"redirect:toMainPage.do";
	}
	@GetMapping("showTip.do")
	public String  aggna(Integer tipId,Model model) {
		log.info("tipId ="+ tipId);
		TipVO tip = tipService.findById(tipId);
		model.addAttribute("tip",tip);
		log.info("tip="+tip);//这里的错 可能哪里打错了= =！
		List<Reply> replies =replyService.findByTip(tipId);
		model.addAttribute("replies",replies);
		return"tipContent";
	}
}
